package com.frcdi.qms.web.controller.system;

import java.io.IOException;
import java.util.List;

import com.frcdi.qms.common.config.QmsConfig;
import com.frcdi.qms.common.exception.file.InvalidExtensionException;
import com.frcdi.qms.common.utils.file.FileUploadUtils;
import com.frcdi.qms.common.utils.file.MimeTypeUtils;
import com.frcdi.qms.masterdata.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.frcdi.qms.common.annotation.Log;
import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.enums.BusinessType;
import com.frcdi.qms.system.domain.QmsMessage;
import com.frcdi.qms.system.service.IQmsMessageService;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * messageController
 *
 * @author Mr.han
 * @date 2024-05-15
 */
@Api(tags = "message")
@RestController
@RequestMapping("/system/message")
public class QmsMessageController extends BaseController
{
    @Autowired
    private IQmsMessageService qmsMessageService;
    @Autowired
    private IFileService fileService;

    /**
     * 查询message列表
     */
    @ApiOperation("查询message列表")
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(QmsMessage qmsMessage)
    {
        startPage();
        List<QmsMessage> list = qmsMessageService.selectQmsMessageList(qmsMessage);
        return getDataTable(list);
    }

    /**
     * 导出message列表
     */
    @ApiOperation("导出message列表")
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsMessage qmsMessage)
    {
        List<QmsMessage> list = qmsMessageService.selectQmsMessageList(qmsMessage);
        ExcelUtil<QmsMessage> util = new ExcelUtil<QmsMessage>(QmsMessage.class);
        util.exportExcel(response, list, "message数据");
    }

    /**
     * 获取message详细信息
     */
    @ApiOperation("获取message详细信息")
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") Long messageId)
    {
        QmsMessage message = qmsMessageService.selectQmsMessageByMessageId(messageId);
        message.setFileList(fileService.selectFilesListByChangeNO(message.getMessageId().toString()));
        return success(message);
    }

    /**
     * 新增message
     */
    @ApiOperation("新增message")
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @PostMapping
    public AjaxResult add(@RequestBody QmsMessage qmsMessage)
    {

        // 上传文件路径
        String uploadPath = QmsConfig.getUploadPath();
        // 上传并返回新文件名称
        try {
            qmsMessage.setCreateBy(getUsername());
            return toAjax(qmsMessageService.insertQmsMessage(qmsMessage));
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改message
     */
    @ApiOperation("修改message")
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody QmsMessage qmsMessage)
    {

        qmsMessage.setUpdateBy(getUsername());
        return toAjax(qmsMessageService.updateQmsMessage(qmsMessage));
    }

    /**
     * 删除message
     */
    @ApiOperation("删除message")
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
	@DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable Long[] messageIds)
    {
        return toAjax(qmsMessageService.deleteQmsMessageByMessageIds(messageIds));
    }
}
